package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.entity.BidFavorite;
import com.apache.bid.manager.BidFavoriteManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;

public class BidFavoriteManagerImpl implements BidFavoriteManager {

	protected IDao bidFavoriteDao;
	protected final String entityName = "com.apache.bid.entity.BidFavorite";

	public void setBidFavoriteDao(IDao bidFavoriteDao) {
		this.bidFavoriteDao = bidFavoriteDao;
	}

	public String saveInfo(ParamsVo<BidFavorite> vo) throws BusinessException {
		BidFavorite bidFavorite = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidFavorite_"+infoId;
		bidFavorite.setFavoriteId(infoId);
		MethodParam param = new MethodParam("BidFavorite", cacheKey, "", entityName);
		param.setVaule(bidFavorite);
		if (bidFavoriteDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidFavorite> vo) throws BusinessException {
		BidFavorite bidFavorite = vo.getObj();
		if (Validator.isNotNull(bidFavorite.getFavoriteId())) {
			String cacheKey = "";
			//String cacheKey="bidFavorite_"+bidFavorite.getFavoriteId();
			MethodParam param = new MethodParam("BidFavorite", cacheKey, "", entityName);
			param.setVaule(bidFavorite);
			return bidFavoriteDao.edit(param);
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidFavorite> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidFavorite_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		BidFavorite info = (BidFavorite) bidFavoriteDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("BidFavorite");
			return bidFavoriteDao.edit(param);
		} else {
			param.setParams("favoriteId", infoId);
			param.setDelete(true);
			return bidFavoriteDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<BidFavorite> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidFavorite_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidFavoriteDao.selectById(param);
	}

	public Object execute(ParamsVo<BidFavorite> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<BidFavorite> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidFavoriteDao.pageSelect(param);
		return page;
	}

	public List<BidFavorite> getList(ParamsVo<BidFavorite> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		return bidFavoriteDao.select(param);
	}

	public long countInfo(ParamsVo<BidFavorite> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidFavoriteDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidFavorite> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidFavoriteDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		param.setResultCache(false);
		BidFavorite bidFavorite = vo.getObj();
		if (Validator.isNotNull(bidFavorite.getItemId())) {
			sb.append(" and itemId = :itemId");
			param.setParams("itemId", bidFavorite.getItemId());
		}
		if (Validator.isNotNull(bidFavorite.getItemName())) {
			sb.append(" and itemName = :itemName");
			param.setParams("itemName", bidFavorite.getItemName());
		}
		if (Validator.isNotNull(bidFavorite.getSessionId())) {
			sb.append(" and sessionId = :sessionId");
			param.setParams("sessionId", bidFavorite.getSessionId());
		}
		if (Validator.isNotNull(bidFavorite.getSessionName())) {
			sb.append(" and sessionName = :sessionName");
			param.setParams("sessionName", bidFavorite.getSessionName());
		}
		if (Validator.isNotNull(bidFavorite.getKeepUrl())) {
			sb.append(" and keepUrl = :keepUrl");
			param.setParams("keepUrl", bidFavorite.getKeepUrl());
		}
		if (Validator.isNotNull(bidFavorite.getCreateUser())) {
			sb.append(" and createUser = :createUser");
			param.setParams("createUser", bidFavorite.getCreateUser());
		}
		if (Validator.isNotNull(bidFavorite.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidFavorite.getCreateTime());
		}
		if (Validator.isNotNull(bidFavorite.getModifyTime())) {
			sb.append(" and modifyTime = :modifyTime");
			param.setParams("modifyTime", bidFavorite.getModifyTime());
		}
		if (Validator.isNotNull(bidFavorite.getStatus())) {
			sb.append(" and status = :status");
			param.setParams("status", bidFavorite.getStatus());
		}
		if (Validator.isNotNull(bidFavorite.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidFavorite.getSpare1());
		}
		if (Validator.isNotNull(bidFavorite.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidFavorite.getSpare2());
		}
		if (Validator.isNotNull(bidFavorite.getSpare3())) {
			sb.append(" and spare3 = :spare3");
			param.setParams("spare3", bidFavorite.getSpare3());
		}
		if (Validator.isNotNull(bidFavorite.getSpare4())) {
			sb.append(" and spare4 = :spare4");
			param.setParams("spare4", bidFavorite.getSpare4());
		}
		if (Validator.isNotNull(bidFavorite.getSpare5())) {
			sb.append(" and spare5 = :spare5");
			param.setParams("spare5", bidFavorite.getSpare5());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
